package com.inst.jsoup_html_demo.fragment;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.inst.jsoup_html_demo.R;
import com.inst.jsoup_html_demo.adapter.ViewpagerAdapter;
import com.inst.jsoup_html_demo.utils.DataGenerator;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by zhouwei on 17/4/23.
 */

public class HomeFragment extends Fragment {
    @BindView(R.id.radio_button_caipu_home)
    RadioButton mRadioButtonCaipuHome;
    @BindView(R.id.radio_button_caipu_fenlei)
    RadioButton mRadioButtonCaipuFenlei;
    @BindView(R.id.radio_button_caipu_shicai)
    RadioButton mRadioButtonCaipuShicai;
    @BindView(R.id.radio_group_button_caipu)
    RadioGroup mRadioGroupButtonCaipu;
    @BindView(R.id.iv_add)
    ImageView mIvAdd;
    @BindView(R.id.tv_facaipu)
    TextView mTvFacaipu;
    @BindView(R.id.rl_facaifu)
    RelativeLayout mRlFacaifu;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.view_pager_home)
    ViewPager mViewPagerhome;
    private ViewpagerAdapter mAdapter;
    Unbinder unbinder;
    private ArrayList<Fragment> mFragments;
    private RadioButton mRadioButtonHome;
    private String mFrom;

    public static HomeFragment newInstance(String from) {
        HomeFragment fragment = new HomeFragment();
        Bundle bundle = new Bundle();
        bundle.putString("from", from);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mFrom = getArguments().getString("from");
        }
        Log.e("HomeFragment--", "onCreate：" + mFrom);
    }
    View view;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mFragments == null) {
            mFragments = DataGenerator.getCaipuFragments("RadioGroup___ Tab");
        }
        view = inflater.inflate(R.layout.home_fragment_layout, null);
        unbinder = ButterKnife.bind(this, view);
        initView(view);
        Log.e("HomeFragment--", "onCreateView");
        return view;
    }

    private void initView(View view) {
        //初始化标题栏字体大小
        chageFont(0);
        if(mAdapter!=null){
            mAdapter.notifyDataSetChanged();
        }else{
            mAdapter = new ViewpagerAdapter(this.getFragmentManager(), mFragments);
            mViewPagerhome.setAdapter(mAdapter);
        }
        mViewPagerhome.setCurrentItem(0, true);// 初始化，并去掉切换页面的动画
        mRadioGroupButtonCaipu.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.radio_button_caipu_home:
                        chageFont(0);
                        mViewPagerhome.setCurrentItem(0, true);// 去掉切换页面的动画
                    break;
                    case R.id.radio_button_caipu_fenlei:
                        chageFont(1);
                        mViewPagerhome.setCurrentItem(1, true);// 去掉切换页面的动画
                        break;
                    case R.id.radio_button_caipu_shicai:
                        chageFont(2);
                        mViewPagerhome.setCurrentItem(2, true);// 去掉切换页面的动画
                        break;
                }
            }
        });
        mViewPagerhome.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mFragments.get(position);// 获取当前被选中的页面, 初始化该页面数据
                chageFont(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /***
     *  字体切换
     */
    public void chageFont(int position) {
        if (position == 0) {
            mRadioButtonCaipuHome.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_8));
            mRadioButtonCaipuFenlei.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_9));
            mRadioButtonCaipuShicai.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_9));
        } else if (position == 1) {
            mRadioButtonCaipuHome.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_9));
            mRadioButtonCaipuFenlei.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_8));
            mRadioButtonCaipuShicai.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_9));
        } else if (position == 2) {
            mRadioButtonCaipuHome.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_9));
            mRadioButtonCaipuFenlei.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_9));
            mRadioButtonCaipuShicai.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_8));
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        Log.e("HomeFragment--", "onResume");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        mFragments.clear();
        mFragments = null;
        Log.e("HomeFragment--", "onDestroyView");
    }
}
